package com.wyp.didishige.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.wyp.didishige.R;
import com.wyp.didishige.utils.ImageLoaderUtil;

import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

/**
 * 用户增删个人图片适配器
 * Created by horry on 2015/10/17.
 */
public class UserSetImageGridAdapter extends BaseAdapter {
    private Context mContext;

    private List<Object> objectList;
    public UserSetImageGridAdapter(Context context, List<Object> list) {
        super();
        this.mContext = context;
        this.objectList = list;
    }

    public void addPhotoImage(String info){
        objectList.add(info);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        objectList.remove(position);
        notifyDataSetChanged();
    }

    public void remove(Object object){
        objectList.remove(object);
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
            return objectList.size();
    }

    @Override
    public Object getItem(int position) {
            return objectList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.request_image_item, null);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Object object = objectList.get(position);

        if (object instanceof String) {
            BitmapDrawable photoInfo = new BitmapDrawable((String) object);
            holder.image.setImageDrawable(photoInfo);
        } else {
            BmobFile image= (BmobFile) object;
            String url = image.getUrl();
            ImageLoaderUtil.ImageLoaderInstance.displayImage(url,holder.image,ImageLoaderUtil.Thumbnail_Image_Options);
        }

        return convertView;
    }

    class ViewHolder {
        ImageView image;
    }
}